package controller

import (
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/service"
	"log"
	"net/http"
)

func (*DeptApi) DeptHandle(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		DeptAPI.deptById(w, r)
		break
	case http.MethodPost:
		DeptAPI.deptAdd(w, r)
		break
	case http.MethodPut:
		DeptAPI.deptUpdate(w, r)
		break
	case http.MethodDelete:
		DeptAPI.deptDeleteBatch(w, r)
		break
	default:
		break
	}
}

func (*DeptApi) DeptTree(w http.ResponseWriter, r *http.Request) {
	deptTree, err := service.DeptTree(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, deptTree)
}

// 根据ID查询数据
func (*DeptApi) deptById(w http.ResponseWriter, r *http.Request) {
	page, err := service.DeptById(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// DeptPage 分页查询
func (*DeptApi) DeptPage(w http.ResponseWriter, r *http.Request) {
	page, err := service.DeptPage(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// 新增数据
func (*DeptApi) deptAdd(w http.ResponseWriter, r *http.Request) {
	add, err := service.DeptAdd(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 批量删除数据
func (*DeptApi) deptDeleteBatch(w http.ResponseWriter, r *http.Request) {
	add, err := service.DeptDeleteBatch(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 根据ID更新数据
func (*DeptApi) deptUpdate(w http.ResponseWriter, r *http.Request) {
	add, err := service.DeptUpdate(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// DeptList 查询所有数据
func (*DeptApi) DeptList(w http.ResponseWriter, r *http.Request) {
	deptList, err := service.DeptList(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, deptList)
}

// 激活禁用(部门)
func (*DeptApi) Active(w http.ResponseWriter, r *http.Request) {
	active, err := service.ActiveDept(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, active)
}
